/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	OptionalClassInfoMgr.java
 * 模块说明：	
 * 修改历史：
 * 2007-3-15 - lxm - 创建。
 */
package com.hd123.rumba.option;

import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.Manager;

/**
 * 选项类信息 | 管理员 | 实现类
 * 
 * @author lxm
 * 
 */
public class OptionalClassInfoMgr extends Manager implements IOptionalClassInfoMgr {

  public static final String CAPTION_MANAGER = "选项类信息管理员";

  public OptionalClassInfoMgr() {
    super();
    this.registryClass(IOptionalClassInfo.class.getName(), OptionalClassInfo.class.getName());
  }

  public IOptionalClassInfo createClassInfo() throws BusinessException {
    IOptionalClassInfo classInfo = new OptionalClassInfo();
    return classInfo;
  }

  public void saveClassInfo(IOptionalClassInfo classInfo) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(classInfo, "classInfo");
    verifyToSave(classInfo);
    getSession().saveOrUpdate(classInfo);
  }

  public void removeClassInfo(IOptionalClassInfo classInfo) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(classInfo, "classInfo");
    getSession().delete(classInfo);
  }

  public IOptionalClassInfo getClassInfo(String uuid) throws BusinessException, HibernateException {
    this.checkParameterNotNull(uuid, "uuid");
    return (IOptionalClassInfo) getSession().get(IOptionalClassInfo.class, uuid);
  }

  public IOptionalClassInfo getClassInfoByClass(String optionalClassName) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(optionalClassName, "optionalClassName");
    List list = getSession().find(
        "from " + IOptionalClassInfo.class.getName() + " oci" + " where oci.optionalClassName = ?",
        optionalClassName, Hibernate.STRING);
    if (list.size() > 0)
      return (IOptionalClassInfo) list.get(0);
    else
      return null;
  }

  public IOptionalClassInfo fetchClassInfoByClass(String optionalClassName)
      throws BusinessException, HibernateException {
    IOptionalClassInfo classInfo = this.getClassInfoByClass(optionalClassName);
    if (classInfo == null)
      classInfo = this.createClassInfo();
    return classInfo;
  }

  private void verifyToSave(IOptionalClassInfo classInfo) throws BusinessException,
      HibernateException {
    assert classInfo.getOptionalClassName() != null;
    IOptionalClassInfo classInfo2 = this.getClassInfoByClass(classInfo.getOptionalClassName());
    if (classInfo2 != null && !classInfo2.getUuid().equals(classInfo.getUuid()))
      throw new BusinessException("已经存在相同选项类(" + classInfo.getOptionalClassName() + ")的注册信息。");
  }

}
